/**
 * 设备检测工具函数
 * 用于检测设备类型、性能等级和支持的功能
 */

/**
 * 检测是否为移动设备
 * @returns {boolean} 是否为移动设备
 */
export function isMobileDevice() {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
}

/**
 * 检测是否为触摸设备
 * @returns {boolean} 是否支持触摸
 */
export function isTouchDevice() {
  return 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0
}

/**
 * 检测是否为平板设备
 * @returns {boolean} 是否为平板
 */
export function isTabletDevice() {
  const userAgent = navigator.userAgent.toLowerCase()
  return /ipad|tablet|(android(?!.*mobile))|(windows(?!.*phone)(.*touch))|kindle|playbook|silk|(puffin(?!.*(IP|AP|WP)))/.test(userAgent)
}

/**
 * 检测设备性能等级
 * @returns {string} 性能等级：'high', 'medium', 'low'
 */
export function getDevicePerformance() {
  // 检测硬件并发数
  const cores = navigator.hardwareConcurrency || 2
  
  // 检测内存（如果支持）
  const memory = navigator.deviceMemory || 2
  
  // 检测连接类型（如果支持）
  const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection
  const effectiveType = connection?.effectiveType || '4g'
  
  // 综合评分
  let score = 0
  
  // CPU 核心数评分
  if (cores >= 8) score += 3
  else if (cores >= 4) score += 2
  else score += 1
  
  // 内存评分
  if (memory >= 8) score += 3
  else if (memory >= 4) score += 2
  else score += 1
  
  // 网络评分
  if (effectiveType === '4g') score += 2
  else if (effectiveType === '3g') score += 1
  
  // 移动设备降级
  if (isMobileDevice() && !isTabletDevice()) {
    score -= 1
  }
  
  if (score >= 7) return 'high'
  if (score >= 4) return 'medium'
  return 'low'
}

/**
 * 检测是否支持 WebGL
 * @returns {boolean} 是否支持 WebGL
 */
export function supportsWebGL() {
  try {
    const canvas = document.createElement('canvas')
    return !!(window.WebGLRenderingContext && canvas.getContext('webgl'))
  } catch (e) {
    return false
  }
}

/**
 * 检测是否支持 OffscreenCanvas
 * @returns {boolean} 是否支持 OffscreenCanvas
 */
export function supportsOffscreenCanvas() {
  return typeof OffscreenCanvas !== 'undefined'
}

/**
 * 检测是否支持 requestIdleCallback
 * @returns {boolean} 是否支持 requestIdleCallback
 */
export function supportsRequestIdleCallback() {
  return typeof requestIdleCallback !== 'undefined'
}

/**
 * 检测用户是否偏好减少动画
 * @returns {boolean} 是否偏好减少动画
 */
export function prefersReducedMotion() {
  return window.matchMedia('(prefers-reduced-motion: reduce)').matches
}

/**
 * 获取设备信息摘要
 * @returns {object} 设备信息对象
 */
export function getDeviceInfo() {
  return {
    isMobile: isMobileDevice(),
    isTouch: isTouchDevice(),
    isTablet: isTabletDevice(),
    performance: getDevicePerformance(),
    supportsWebGL: supportsWebGL(),
    supportsOffscreenCanvas: supportsOffscreenCanvas(),
    supportsRequestIdleCallback: supportsRequestIdleCallback(),
    prefersReducedMotion: prefersReducedMotion(),
    userAgent: navigator.userAgent,
    cores: navigator.hardwareConcurrency || 'unknown',
    memory: navigator.deviceMemory || 'unknown',
    connection: navigator.connection?.effectiveType || 'unknown'
  }
}

/**
 * 获取推荐的动画配置
 * @returns {object} 动画配置对象
 */
export function getRecommendedAnimationConfig() {
  const performance = getDevicePerformance()
  const isMobile = isMobileDevice()
  const reducedMotion = prefersReducedMotion()
  
  if (reducedMotion) {
    return {
      enableParticles: false,
      enableMouseCursor: false,
      enableLottieAnimations: false,
      particleCount: 0,
      animationDuration: 0
    }
  }
  
  if (performance === 'low' || (isMobile && !isTabletDevice())) {
    return {
      enableParticles: false,
      enableMouseCursor: false,
      enableLottieAnimations: true,
      particleCount: 0,
      animationDuration: 200
    }
  }
  
  if (performance === 'medium') {
    return {
      enableParticles: true,
      enableMouseCursor: isMobile ? false : true,
      enableLottieAnimations: true,
      particleCount: isMobile ? 30 : 50,
      animationDuration: 300
    }
  }
  
  // High performance
  return {
    enableParticles: true,
    enableMouseCursor: true,
    enableLottieAnimations: true,
    particleCount: isMobile ? 50 : 100,
    animationDuration: 400
  }
}